// pages/studyFinsh/index.js
const app = getApp();// 云：add this 
const HTTP = require('../../utils/http');
const API = require('../../utils/api');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 学习感受反馈表情src
    srcEasy: "/image/7-learnfeelingeasy-gray.png",
    srcOrdinary:"/image/8-learnfeelingordinary-gray.png",
    srcHard:"/image/9-learnfeelinghard-gray.png",

    safeHeight: app.globalData.tabBarHeight, // 云：add this before every page //
    safeTop: app.globalData.safeTop,
    menuTop: app.globalData.menuTop,
    menuHeight: app.globalData.menuHeight,
    safeBottom: app.globalData.safeBottom, // 云：add this before every page //
    // courseId:"1638143225841795074"
    courseId:"",
    courseData: {},
    userInfo:{},
    mineStatistic:{},
    stared: false,
    liked: false
  },

  // 点击学习感受
  tapFeelingEmotionCard(event){
    console.log("点击学习感受表情", event.currentTarget.dataset)
    if(this.data.srcEasy==="/image/7-learnfeelingeasy.png"||this.data.srcOrdinary==="/image/8-learnfeelingordinary.png"||this.data.srcHard==="/image/9-learnfeelinghard.png"){
      wx.showToast({
        title: '您已反馈！',
        icon:'error'
      })
    }
    else{
      if(event.currentTarget.dataset.id==0){
        this.setData({
          srcEasy:"/image/7-learnfeelingeasy.png"
        })
      }
      else if(event.currentTarget.dataset.id==1){
        this.setData({
          srcOrdinary:"/image/8-learnfeelingordinary.png"
        })
      }
      else{
        this.setData({
          srcHard:"/image/9-learnfeelinghard.png"
        })
      }
      wx.showToast({
        title: '反馈成功！',
        icon:'success'
      })
    }
    
  },

  tapLike(){
    if(this.data.liked){
      let that=this
      wx.showModal({
        title: '取消点赞',
        content: '您确定需要取消点赞吗？',
        success (res) {
          if (res.confirm) {
            console.log('用户点击确定取消点赞')
            that.likeTrueOrFalse(
              {
                id: that.data.courseData.id,
                liked: false
              }
            )
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
    else{
      this.likeTrueOrFalse(
        {
          id: this.data.courseData.id,
          liked: true
        }
      )
    }
  },

  // 确定或者取消点赞
  likeTrueOrFalse(data){
    wx.request({
      url: API.LikeCourse,
      header:{
        "Authorization": 'Bearer '+wx.getStorageSync('accessTokenData').accessToken
      },
      method: "put",
      data:data,
      success: (res) => {
        console.log("调用点赞接口返回的信息是：",res)
        if(data.liked){
          if(res.data.code===0){
            wx.showToast({
              title: '点赞成功',
            })
            this.setData({
              liked: true
            })
          }
          else{
            wx.showToast({
              title: '点赞出错',
            })
          }
        }
        else{
          if(res.data.code===0){
            wx.showToast({
              title: '取消点赞成功',
            })
            this.setData({
              liked: false
            })
          }
          else{
            wx.showToast({
              title: '取消点赞出错',
            })
          }
        }
      },
      fail: (err) => {
        wx.showModal({
          title: '提示',
          content: JSON.stringify(err),
        })
        console.log(err);
      },
      complete:()=>{
        
      }
    })
  },

  // 确定或者取消收藏
  starTrueOrFalse(data){
    wx.request({
      url: API.StarCourse,
      header:{
        "Authorization": 'Bearer '+wx.getStorageSync('accessTokenData').accessToken
      },
      method: "put",
      data:data,
      success: (res) => {
        console.log("调用收藏接口返回的信息是：",res)
        if(data.stared){
          if(res.data.code===0){
            wx.showToast({
              title: '收藏成功',
            })
            this.setData({
              stared: true
            })
          }
          else{
            wx.showToast({
              title: '收藏出错',
            })
          } 
        }
        else{
          if(res.data.code===0){
            wx.showToast({
              title: '取消收藏成功',
            })
            this.setData({
              stared: false
            })
          }
          else{
            wx.showToast({
              title: '取消收藏出错',
            })
          }
        }
      },
      fail: (err) => {
        wx.showModal({
          title: '提示',
          content: JSON.stringify(err),
        })
        console.log(err);
      },
      complete:()=>{
        
      }
    })
  },

  tapStar(){
    if(this.data.stared){
      let that=this
      wx.showModal({
        title: '取消收藏',
        content: '您确定需要取消收藏吗？',
        success (res) {
          if (res.confirm) {
            console.log('用户点击确定取消收藏')
            that.starTrueOrFalse(
              {
                id: that.data.courseData.id,
                stared: false
              }
            )
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
    else{
      this.starTrueOrFalse(
        {
          id: this.data.courseData.id,
          stared: true
        }
      )
    } 
  },

  tapShare(){
    console.log("点击页面分享按钮!")
    // wx.showShareMenu()
    wx.showModal({
      title: '分享课程',
      content: '请点击小程序右上角胶囊分享课程!',
      showCancel: false,
      confirmText: '我知道了'
    })
  },

  tapToFeedback(){
    console.log("点击反馈按钮")
    wx.navigateTo({
        url: `/pages/questionfeedback/questionfeedback?id=${this.data.courseId}`,
      })
  },

  tapComeBack(){
    wx.navigateBack({
      delta: 2,
    })
    console.log("点击当前页面的返回按钮",this.data.menuTop)
  },

  tapToCourseList(){
    wx.navigateBack({
      delta: 2,
      success: (res) => {},
      fail: (res) => {},
      complete: (res) => {},
    })
  },
  tapToHome(){
    wx.switchTab({
        url: '/pages/home/home',
    });
  },

//   /**
//    * 用户点击右上角分享
//    */
//   onShareAppMessage: function () {
//     return {
//       title: "完成学习",    //自定义标题   string
//       path: `/pages/store/store?id=${this.data.shop.shop_id}`  //这个地址需要把页面路径拼接的参数发送出去,直写页面地址的话，别人进入会是空的页面
//     }
//   },
//   onShareTimeline: function(res){
//     let img = this.data.path_image+this.data.shop.shop_img  //取得是每个店铺的图和店铺名称
//     console.log(img)
//     return {
//       title: this.data.shop.name, //字符串  自定义标题
//       query: `id=${this.data.shop.shop_id}`,  //页面携带参数
//       imageUrl:img   //图片地址
//     }
//   },



getMineData(){
  wx.request({
    // url: this.globalData.url + '/login',
    url: app.globalData.server+'/app-api/member/user/get',
    header:{
      "Authorization": 'Bearer '+wx.getStorageSync('accessTokenData').accessToken
    },
    method: "GET",
    success: (res) => {
      console.log("根据accessToken获取的用户信息是：",res)
      const data = res.data;
      this.setData({
        userInfo: data.data,
      })
      this.getUserStatistic()
    },
    fail: (err) => {
      wx.showModal({
        title: '提示',
        content: JSON.stringify(err),
      })
      console.log(err);
    },
    complete:()=>{
      wx.hideLoading();
    }
  })
},

getUserStatistic(){
  wx.request({
    // url: this.globalData.url + '/login',
    url: app.globalData.server+'/app-api/epg/user/statistic',
    header:{
      "Authorization": 'Bearer ' + wx.getStorageSync('accessTokenData').accessToken
    },
    method: "GET",
    success: (res) => {
      console.log("根据accessToken获取的用户学习数据是：",res)
      if(res.data.code===0){
        let mineStatisticTemp = res.data.data
        mineStatisticTemp.rank = parseInt(mineStatisticTemp.rank*100)
        this.setData({
          mineStatistic: mineStatisticTemp,
        })
      }
      
    },
    fail: (err) => {
      console.log(err);
    },
    complete:()=>{
      wx.hideLoading();
    }
  })
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('进入学习完成界面，参数是 :', options.id)
    this.setData({
      courseId: options.id
    })
    wx.request({
      url: API.GetCourseDetail,
      header:{
        "Authorization": 'Bearer '+wx.getStorageSync('accessTokenData').accessToken
      },
      method: "get",
      data: {
        id:options.id
      },
      success: (res) => {
        let data = res.data
        console.log('加载课程详情是 :',data)
        this.setData({
          courseData: data.data,
        })
        this.setData({
          stared: data.data.stared,
          liked: data.data.liked,
        })
      },
      fail: (err) => {
        wx.showModal({
          title: '提示',
          content: JSON.stringify(err),
        })
        console.log(err);
      },
      complete:()=>{
        
      }
    })

    // 获取本人数据
    this.getMineData()
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})